package com.system.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.auth.domain.T_function;
import com.auth.domain.T_role;
import com.auth.domain.T_role_function;
import com.auth.domain.T_user;
import com.auth.domain.T_user_function;
import com.auth.domain.T_user_role;
import com.auth.mapper.T_functionMapper;
import com.auth.mapper.T_roleMapper;
import com.auth.mapper.T_role_functionMapper;
import com.auth.mapper.T_userMapper;
import com.auth.mapper.T_user_functionMapper;
import com.auth.mapper.T_user_roleMapper;
import com.auth.web.spring.AuthUtils;
import com.common.db.JsonVO;
import com.common.servlet.AttributeListener;
import com.common.utils.DateUtil;
import com.common.utils.SystemConstants;
import com.common.utils.SystemDataDicCache;
import com.system.domain.T_data_dic;
import com.system.domain.T_login_log;
import com.system.mapper.T_data_dicMapper;
import com.system.mapper.T_login_logMapper;
import com.system.service.LogonService;

/**
 * 登录页面
 *
 * @author davexin
 *
 */
@Controller
public class CountIPController {

	private static Logger log = Logger.getLogger(CountIPController.class.getName());

	@Resource
	private T_login_logMapper mapper;

	//@Transactional(rollbackFor={Exception.class, RuntimeException.class})
	@RequestMapping(value = "/countIP")
	public void countIP(HttpServletRequest request,HttpServletResponse response) {
		log.debug("------记录访问ip等数据 start------");
		
		//设置访问ip信息
		T_login_log loginlog = new T_login_log();

		try{
			loginlog.setLogin_addr(getIp(request));
			loginlog.setLogin_in_tm(DateUtil.getCurrDateTime());
			loginlog.setUserid(""+request.getSession().getAttribute("FREE_CURRENT_USER"));
			loginlog.setLogin_remark("ip访问成功");
			loginlog.setSession_id(request.getSession().getId());
			loginlog.setCrt_tm("ipcount");//设置成固定字符串，标示不同类型访问请求；
			mapper.save(loginlog);
		}catch(Exception e){
			log.error(e);
		}

		log.debug("------记录访问ip等数据 end------");
		//return "";
	}

	//@Transactional(rollbackFor={Exception.class, RuntimeException.class})
	public String getCountIP(HttpServletRequest request,HttpServletResponse response) {
		//设置登出日志信息
		T_login_log loginlog = new T_login_log();
		int ipnum = mapper.countIp(loginlog);
		return ""+ipnum;
	}
	
	public String getCountIPToday(HttpServletRequest request,HttpServletResponse response) {
		//设置登出日志信息
		T_login_log loginlog = new T_login_log();
		loginlog.setLogin_in_tm(DateUtil.getCurrDate()); //yyyyMMdd
		int ipnumToday = mapper.countIpToday(loginlog);
		return ""+ipnumToday;
	}
	
	public String getUser(HttpServletRequest request,HttpServletResponse response) {
		//设置登出日志信息
		T_login_log loginlog = new T_login_log();
		int userNum = mapper.countUser(loginlog);
		return ""+userNum;
	}
	
	@RequestMapping(value = "/getIpCountAndUserCount")
	@ResponseBody
	public JsonVO getIpCountAndUserCount(HttpServletRequest request,HttpServletResponse response) {
		JsonVO vo = new JsonVO();
		vo.setResult("error");
		vo.setJieguo(false);
		
		//设置登出日志信息
		T_login_log loginlog = new T_login_log();
		vo.setJieguo(true);
		vo.setResult("success");
		
	    vo.setInfo(getCountIP(request,response)+";"+getCountIPToday(request,response)+";"+getUser(request,response));
	    return vo;
	}
	
	/**
	 * 获取访问者ip
	 * @param request
	 * @return
	 */
	public String getIp(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}
}
